package cn.edu.ecnu.gis.client.alert.view;

import cn.edu.ecnu.gis.client.alert.model.Contact;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;


public class ContactListView extends SimplePanel{
	
	private FlexTable listTable = new FlexTable();
	private MessengerView view;
	
	public ContactListView( MessengerView view ){
		setWidget( listTable );
		this.view = view;
		listTable.setCellPadding(0);
		listTable.setCellSpacing(0);
		addContact( view.getContactList().getMe() );
		listTable.getRowFormatter().addStyleName(0, "myContactItem" );
		listTable.getColumnFormatter().setWidth(1,"100%");
	}
	
	public void addContact( final Contact contact ){
		
		int row = listTable.getRowCount();
		Hyperlink link = new Hyperlink( contact.getName(), contact.getName() );
		listTable.getRowFormatter().setVerticalAlign(row,HasVerticalAlignment.ALIGN_TOP);
		final Image icon = new Image("icon_user.gif");
		icon.addClickListener(new ClickListener(){
			public void onClick( Widget sender ){
				try{
				RequestBuilder requestBuilder = new RequestBuilder( RequestBuilder.GET, GWT.getModuleBaseURL()+"/"+contact.getName()+".html" );
				requestBuilder.sendRequest( null, new RequestCallback(){
					public void onError(Request request, Throwable exception){ /*ignored*/ }
					public void onResponseReceived(Request request, Response response){
						if( response.getStatusCode() == 200 ){
							PopupPanel popup = new PopupPanel(true);
							popup.setStyleName("popup");
							popup.setWidget( new HTML( response.getText() ));
							popup.setPopupPosition(icon.getAbsoluteLeft(), icon.getAbsoluteTop()+icon.getOffsetHeight());
							popup.show();
						}
					}

				});
				}catch( Exception e){ /*ignored*/ }
			}
		});
		listTable.setWidget(row,0,icon);
		listTable.getRowFormatter().setStyleName(row, "contactItem" );
		if( row != 0 ) listTable.setWidget(row,1,link);
		else listTable.setText(row,1, contact.getName() );
	}
	
	public void removeContact( Contact contact ){
		for( int i=1; i<listTable.getRowCount();  ){
			Hyperlink link = (Hyperlink)listTable.getWidget(i,1);
			if( link.getText().compareTo( contact.getName() ) == 0 ) listTable.removeRow(i);
			else ++i;
		}
	}

}